package cn.weiyang.interceptor;

import cn.weiyang.controller.OrderController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志处理拦截器（该拦截器纯粹是为了测试日志拦截器而写的，并无实际用处，测试其它类时可以先在springmvc.xml中先把有关它的配置先注释掉，以便测试其它类及方法）
 * Created by Administrator on 2017/7/26.
 */
public class LogInterceptor implements HandlerInterceptor{
    /*利用log4j进行测试*/
    private static Logger logger= LogManager.getLogger(LogInterceptor.class);
    private ThreadLocal<Long> time=new ThreadLocal<Long>();
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.debug("进入日志拦截器");
        time.set(System.currentTimeMillis());
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.debug("耗时："+(time.get()-System.currentTimeMillis())+"毫秒");
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.debug("逃出日志拦截器");
    }
}
